# -*- coding: utf-8 -*-
# @Author : Orange
# @File : data_processing.py
import pandas as pd
import time


def data_processing(iv_data):
    # 处理数据,并保存到data.csv文件里,data为字典格式，键为时间戳，值为排气温度
    data = iv_data['data'][0].get('dps')
    # 从上面可以看出，字典无序,按照时间戳(字典的键)排序
    data = sorted(zip(data.keys(), data.values()))
    # 取出排气温度值
    gas_temp = [i[1] for i in data]
    # 将时间戳变为时间序列
    gas_time = [time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(i[0]))) for i in data]
    # 将数据变为Dataframe的格式便于后续处理
    gas_data = pd.DataFrame(gas_temp, index=gas_time, columns=['TcprAirOut'])

    # X的特征名称
    ECR_01_name = ["TcwIn", "Pelec", "STecr", "IratioCpr", "TcwOut", "TchwIn", "TchwOut", "PArefEva", "DURrun",
                   "PAlube",
                   "PArefCond"]

    # 从iv_data中取出所有特征数据，并合并为一个大的dataframe
    for i in range(1, len(iv_data['data'])):
        x = iv_data['data'][i].get('dps')
        x = sorted(zip(x.keys(), x.values()))
        # 取出特征值
        x1 = [i[1] for i in x]
        # 对应的索引
        x1_index = [time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(i[0]))) for i in x]  # 将时间戳变为时间序列
        gas_data1 = pd.DataFrame(x1, index=x1_index, columns=[ECR_01_name[i - 1]])
        gas_data = pd.merge(gas_data1, gas_data, left_index=True, right_index=True, how='outer')
    # nan处理
    gas_data = gas_data.dropna()
    # 丢掉Pelec=0的行，功率为0说明没有启动，对训练无意义
    index = gas_data.Pelec[gas_data.Pelec == 0].index
    data_p0 = gas_data['TcprAirOut'].loc[index]
    gas_data = gas_data.drop(index=gas_data.Pelec[gas_data.Pelec == 0].index)

    return gas_data, data_p0
